package com.mairuide.bill.utils;

import com.mairuide.bill.DealFlow;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: zyj
 * @Date: 2019/5/13 15:26
 * @Description:
 */
public class AnalysisPDFUtils {
    public static List<DealFlow> pdfTemplate1(String[] split, String caseId, Integer createId, String uuid) {
        List<DealFlow> list = new ArrayList<>();
        String[] colnums1 = {"查询账号", "内部结算账号", "交易日期", "交易时间", "交易金额", "交易后余额", "交易产品号"};
        String[] colnums2 = {"对方账号及户名", "传票号", "日志号", "交易码", "交易行号", "交易地点", "摘要"};
        int errorNum = 0;
        try {
//            String[] split = content.split("\r\n");
            for (int i = 0; i < split.length - 1; i++) {
                if (split[i].contains("第") && split[i].contains("页")) {
                    continue;
                }
                if (split[i].contains("[动态查询]")) {
                    i += 2;
                    continue;
                }

                String[] split1 = split[i].split(" ");
                Map<String, String> map = new HashMap<>();
                if (split1.length == 7) {
                    for (int j = 0; j < split1.length; j++) {
                        map.put(colnums1[j], split1[j]);
                    }
                } else {
                    errorNum++;
                    i++;
                    continue;
                }

                String[] split2 = split[i + 1].split(" ");
                if (split2.length == 7) {
                    for (int j = 0; j < split2.length; j++) {
                        map.put(colnums2[j], split2[j]);
                    }
                } else {
                    errorNum++;
                    i++;
                    continue;
                }

                DealFlow dealFlow = new DealFlow();
                dealFlow.setUuid(uuid);
                dealFlow.setCasemanageId(caseId);
                dealFlow.setCreateId(createId);
                dealFlow.setCreateTime(new Timestamp(System.currentTimeMillis()));
                dealFlow.setIsDelete(0);
                dealFlow.setAccountBodyNumber(map.get("查询账号"));
                dealFlow.setAccountBodyCard(map.get("内部结算账号"));
                String stadate = map.get("交易日期") + " " + map.get("交易时间");
                Date date = DateUtils.getDate(stadate);
                dealFlow.setTradeDate(new Timestamp(date.getTime()));
                dealFlow.setAccountMoney(new BigDecimal(map.get("交易金额")));
                dealFlow.setSurplusMoney(new BigDecimal(map.get("交易后余额")));
                Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]+|\\d+");
                Matcher m = p.matcher(map.get("对方账号及户名"));
                while (m.find()) {
                    String group = m.group();
                    if (isInteger(group)) {
                        dealFlow.setAccountRivalCard(group);
                    } else {
                        dealFlow.setAccountRival(group);
                    }
                }

                dealFlow.setSerialNumber(map.get("交易码"));
                dealFlow.setAccountAddress(map.get("交易地点"));
                dealFlow.setContentAbstract(map.get("摘要"));

                list.add(dealFlow);
                i++;
            }

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    public static List<DealFlow> pdfTemplate2(String[] split, String caseId, Integer createId, String uuid) {
        List<DealFlow> list = new ArrayList<>();
        String body = split[0].split(" ")[1];
        String bodyNumber = split[0].split(" ")[2].split("：")[1];
        int totalNumber = split[1].split(" ").length;
        try {
            if (totalNumber==8) {
                for (int i = 2; i < split.length; i++) {
                    String[] split1 = split[i].split(" ");
                    DealFlow dealFlow = new DealFlow();
                    dealFlow.setUuid(uuid);
                    dealFlow.setCasemanageId(caseId);
                    dealFlow.setCreateId(createId);
                    dealFlow.setCreateTime(new Timestamp(System.currentTimeMillis()));
                    dealFlow.setIsDelete(0);
                    dealFlow.setAccountBody(body);
                    dealFlow.setAccountBodyNumber(bodyNumber);
                    dealFlow.setAccountBodyCard(bodyNumber);
                    Date date = DateUtils.getDate(split1[0] + " " + split1[1]);
                    if (date!=null) dealFlow.setTradeDate(new Timestamp(date.getTime()));
                    dealFlow.setAccountMoney(new BigDecimal(split1[2]));
                    dealFlow.setSurplusMoney(new BigDecimal(split1[3]));
                    if (isInteger(split1[4])){
                        dealFlow.setAccountRivalNumber(split1[4]);
                        dealFlow.setAccountRivalCard(split1[4]);
                    }
                    if (split1.length==8){
                        dealFlow.setAccountRivalBank(split1[5]);
                        dealFlow.setContentAbstract(split1[6]);
                        dealFlow.setAccountRival(split1[7]);
                    }
                    list.add(dealFlow);
                }
            }else if (totalNumber==7){
                for (int i = 2; i < split.length; i++) {
                    String[] split1 = split[i].split(" ");
                    DealFlow dealFlow = new DealFlow();
                    dealFlow.setUuid(uuid);
                    dealFlow.setCasemanageId(caseId);
                    dealFlow.setCreateId(createId);
                    dealFlow.setCreateTime(new Timestamp(System.currentTimeMillis()));
                    dealFlow.setIsDelete(0);
                    dealFlow.setAccountBody(body);
                    dealFlow.setAccountBodyNumber(bodyNumber);
                    dealFlow.setAccountBodyCard(bodyNumber);
                    Date date = DateUtils.getDate(split1[0] + " " + split1[1]);
                    if (date!=null) dealFlow.setTradeDate(new Timestamp(date.getTime()));
                    dealFlow.setAccountMoney(new BigDecimal(split1[2]));
                    dealFlow.setSurplusMoney(new BigDecimal(split1[3]));
                    if (split1.length==7){
                        dealFlow.setAccountRivalBank(split1[4]);
                        dealFlow.setContentAbstract(split1[5]);
                        dealFlow.setAccountRival(split1[6]);
                    }
                    list.add(dealFlow);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }
    /**
     * 判断字符串是否为数字
     * @param str
     * @return
     */
    private static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }
}
